package crawler.lingmu;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zql.entity.AgencyEntity;

import dao.AgencyDao;
import util.HttpConnectionGet;
import util.Location;
import util.MybatisTool;
import util.TianYanCha;

public class LingMuQiCheCrawler {
	public List<AgencyEntity> getAgency() {
		List<AgencyEntity> list = new ArrayList<AgencyEntity>();
		String getPro = "http://www.suzuki-china.com/service/dealer/";
		try {
			Document proDoc = Jsoup.connect(getPro).get();
			Element proSelect = proDoc.getElementById("choice");
			Elements proOptions = proSelect.getElementsByTag("option");
			for(int i=1;i<proOptions.size();i++) {
				String proNum = proOptions.get(i).attr("value");
				String proName = proOptions.get(i).text();
				//System.out.println(proNum+":"+proName);
				
				
				String getCity = "http://www.suzuki-china.com/data/cities/get_by_province?province="+proNum;
				String cityText = HttpConnectionGet.getJson(getCity);
				JSONObject cityJSON = JSON.parseObject(cityText);
				JSONArray cities = cityJSON.getJSONArray("results");
				for(int j=0;j<cities.size();j++) {
					JSONObject city = cities.getJSONObject(j);
					String cityNum = city.getString("id");
					String cityName = city.getString("name");
					//System.out.println(cityNum+":"+cityName);
					
					String getDealer = "http://www.suzuki-china.com/data/shops/getstorelist?province="+proNum+"&city="+cityNum+"&type=1&cars=";
					String dealerText = HttpConnectionGet.getJson(getDealer);
					JSONObject dealerJSON = JSON.parseObject(dealerText);
					JSONArray dealers = dealerJSON.getJSONArray("results");
					for(int k=0;k<dealers.size();k++) {
						JSONObject dealer = dealers.getJSONObject(k);
						String name = dealer.getString("name");
						String address = dealer.getString("address");
						String sellTell = dealer.getString("phone");
						String[] lngAndLat = Location.getLocation(address);
						String lng = lngAndLat[0];
						String lat = lngAndLat[1];
						List<String> shareholder = TianYanCha.getShareholder(name);
						String sControllingShareholder = shareholder.get(0);
						String sOtherShareholders = shareholder.get(1);
						AgencyEntity agency = new AgencyEntity();
						agency.setdCloseDate(null);
						agency.setdOpeningDate(null);
						agency.setdUpdateTime(new Timestamp(System.currentTimeMillis()));
						agency.setnBrandID(-1);
						agency.setsBrand("铃木");

						agency.setnDealerIDWeb(-1);
						agency.setnManufacturerID(-1);
						agency.setsManufacturer("铃木汽车");

						agency.setnState(1);
						agency.setsAddress(address);
						agency.setsCity(cityName);
						agency.setsCustomerServiceCall(null);
						agency.setsDealerName(name);
						agency.setsDealerType(null);
						agency.setsProvince(proName);
						agency.setsSaleCall(sellTell);
						agency.setsLongitude(lng);
						agency.setsLatitude(lat);
						agency.setsControllingShareholder(sControllingShareholder);
						agency.setsOtherShareholders(sOtherShareholders);
						list.add(agency);
						
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	public static void main(String[] args) {
		LingMuQiCheCrawler crawler = new LingMuQiCheCrawler();
		System.out.println("爬虫开始...");
		List<AgencyEntity> agencys = crawler.getAgency();
		System.out.println("抓取完毕,正在存库");
		for(AgencyEntity agency : agencys) {
			MybatisTool.save(agency);
		}
		MybatisTool.close();
		System.out.println("请查看数据库");
	}

}
